<?php

function getWaitingAgreements($BDD_connexion,$mail,$ownerId,$locationId)
{
	
	$dates = array();
	
	$category = getUserRights($BDD_connexion,$ownerId);
	$category = ucfirst($category->label);
	
	if (!$fp = fopen($category."/".$ownerId."/alert.txt","r+")) {
			
		return $dates;

	}

	else {
		
		while(!feof($fp)) {
			
				$Ligne = fgets($fp);
				
				$datas = explode(":#",$Ligne);
				$type = $datas[0];
				
				if ($type == "Ycnt")
				{
				
					$mail2 = $datas[2];
					$datas2 = explode("|",$datas[4]);
					
					if ($mail2 == $mail && intval($datas2[1]) == $locationId)
					{
						$thisDates = array(	"id_location" => $datas2[1],
											"begin" => $datas[3],
											"end"	=> $datas2[0]);
						$dates[] = $thisDates;
					}
					
				}
				
		}
		fclose($fp);		
		
	}
	
	return $dates;
	
}

function addAlertToUser($BDD_connexion,$userId,$id,$type)
{
	
	$category = getUserRights($BDD_connexion,$userId);
	$category = ucfirst($category->label);
	
	if (!(is_dir($category."/".$userId)))
	{
		mkdir($category."/".$userId);
		chmod($category."/".$userId,0777);
	}
	
	if (!is_file($category."/".$userId."/alert.txt"))
	{
		$fp = fopen($category."/".$userId."/alert.txt", 'w');
		fclose($fp);
		chmod($category."/".$userId."/alert.txt",0777);
	}
	
	if (is_dir($category."/".$userId) && is_file($category."/".$userId."/alert.txt"))
	{
		
		if (!$fp = fopen($category."/".$userId."/alert.txt","a+")) {
			
			return false;

		}
		else {
			fputs ($fp, $type."|".$id."\n");
			fclose($fp);
		}
		
	}
	
	return true;
	
}

function deleteAlert($BDD_connexion,$userId,$id_location,$type)
{
	
	$category = getUserRights($BDD_connexion,$userId);
	$category = ucfirst($category->label);
	
	if (!$fp = fopen($category."/".$userId."/alert.txt","r+")) {
			
		return false;

	}

	else {
		
		$Lignes=file($category."/".$userId."/alert.txt");
		fclose($fp);
		
		$fp = fopen($category."/".$userId."/alert.txt","w");
		foreach ($Lignes as $Ligne)
		{
			if ($Ligne != $type."|".$id_location."\n" && $Ligne != $type."|".$id_location."\r\n")
			{
				fwrite($fp,$Ligne);
			}
		}
		fclose($fp);		
		
	}
	
	return true;
	
}

function getAlerts($BDD_connexion)
{
	
	$alerts = array();
	
	$category = getUserRights($BDD_connexion,$_SESSION['userid']);
	$category = ucfirst($category->label);
	
	if (is_dir($category."/".$_SESSION['userid']) && is_file($category."/".$_SESSION['userid']."/alert.txt"))
	{
		
		if (!$fp = fopen($category."/".$_SESSION['userid']."/alert.txt","r")) {
			
			die("Echec de l'ouverture du fichier");

		}

		else {
			while(!feof($fp)) {
			
				$Ligne = fgets($fp);
				if ($Ligne != "\n" && $Ligne != "")
				{
					$content = $Ligne;
					$contentexploded = explode("|",$content);
					
					switch ($contentexploded[0]) {
						case "int":
							$alert = getAlertIntermediaire($BDD_connexion,$contentexploded[1]);
						break;
						case "rdv":
							$alert = getRendezVousInformations($BDD_connexion,$contentexploded[1]);
						break;
						case "Yint":
						case "Nint":
							$alert = getMiniIntermediaire($BDD_connexion,$contentexploded[1]);
						break;
						case "Yloc":
							$alert = getContractInformations($BDD_connexion,$contentexploded[1]);
						break;
						case "MMDoc":
						break;
						default:
							$alert = getAlertLocationInformations($BDD_connexion,$contentexploded[1]);
					}
					
					$alert->type = $contentexploded[0];
					$alerts[] = $alert;
				}

			}
			fclose($fp);
		}
		
	}
	
	return $alerts;

}

function isGoodAlert($alerts,$testedAlert,$testedAlertType)
{
	foreach ($alerts as $alert)
	{
		if ($alert->type == $testedAlertType && intval($alert->location_id) == $testedAlert)
		{
			return true;
		}		
	}
	return false;
}

function getContractInformations($BDD_connexion, $id_location)
{

	//Dans un premier temps il nous faut l'id de la ref_personne de la location
	$requestId = "SELECT ref_personne FROM location WHERE id_location = :id";
	$prepareId = $BDD_connexion->prepare($requestId);
	$prepareId->bindValue(':id',$id_location,PDO::PARAM_INT);
	$prepareId->execute();
	$id = $prepareId->fetch(PDO::FETCH_OBJ);
	
	//Ensuite on doit savoir avec quelle personne on s'arrange.
	if ($_SESSION['userid'] == $id->ref_personne)
	{
		//Dans ce cas on est le locataire, donc les infos sur la personne à rechercher sont connues suivant le propriétaire du bien donc le ref_personne du bien.
		$request = "SELECT personne.id AS pers_id, personne.civilite AS pers_civilite, personne.nom AS pers_nom, personne.prenom AS pers_prenom FROM personne,location WHERE location.id_location = :id AND personne.id = (SELECT ref_proprio FROM bien WHERE bien.id_bien = location.ref_bien)";
	}
	else
	{
		//C'est le propriétaire qui regarde l'alerte, donc il voit les informations du ref_personne simplement.
		$request = "SELECT personne.id AS pers_id, personne.civilite AS pers_civilite, personne.nom AS pers_nom, personne.prenom AS pers_prenom FROM personne WHERE id = (SELECT ref_personne FROM location WHERE id_location = :id)";
	}

	$prepare=$BDD_connexion->prepare($request);
	$prepare->bindValue(':id',$id_location,PDO::PARAM_INT);
	$prepare->execute();
	$alert = $prepare->fetch(PDO::FETCH_OBJ);
	
	//Enfin on va ajouter des détails sur le nom du bien, la durée et le début de la location.
	$requestLoc = "SELECT bien.nom AS location_nom, date_debut, duree FROM bien,location WHERE location.id_location = :id AND bien.id_bien = location.ref_bien";
	$prepareLoc = $BDD_connexion->prepare($requestLoc);
	$prepareLoc->bindValue(':id',$id_location,PDO::PARAM_INT);
	$prepareLoc->execute();
	$datas = $prepareLoc->fetch(PDO::FETCH_OBJ);
	
	$alert->location_nom = $datas->location_nom;
	$alert->date_debut = $datas->date_debut;
	$alert->duree = $datas->duree;
	
	$alert->location_id = $id_location;
	
	return $alert;
	
}

function getAlertIntermediaire($BDD_connexion, $id_bien)
{
	
	$request = "SELECT personne.id AS pers_id, personne.civilite AS pers_civilite, personne.nom AS pers_nom, personne.prenom AS pers_prenom, bien.nom AS location_nom FROM personne,bien,location WHERE location.ref_bien='".$id_bien."' AND personne.id = bien.ref_proprio";
	$prepare=$BDD_connexion->prepare($request);
	$prepare->execute();
	$alert = $prepare->fetch(PDO::FETCH_OBJ);
	$alert->location_id = $id_bien;
	
	return $alert;
	
}

function getRendezVousInformations($BDD_connexion, $id_location)
{
	
	$request = "SELECT personne.id AS pers_id, personne.civilite AS pers_civilite, personne.nom AS pers_nom, personne.prenom AS pers_prenom, bien.nom AS location_nom FROM personne,bien,location WHERE location.id_location='".$id_location."' AND location.ref_bien = bien.id_bien AND personne.id = bien.ref_proprio";
	$prepare=$BDD_connexion->prepare($request);
	$prepare->execute();
	$alert = $prepare->fetch(PDO::FETCH_OBJ);
	$alert->location_id = $id_location;
	
	return $alert;
	
}

function getMiniIntermediaire($BDD_connexion, $id_bien)
{
	
	$request = "SELECT personne.id AS pers_id, personne.civilite AS pers_civilite, personne.nom AS pers_nom, personne.prenom AS pers_prenom, personne.login AS pers_login, bien.nom AS bien_nom FROM personne,bien WHERE bien.id_bien='".$id_bien."' AND personne.id = bien.ref_intermediaire";
	$prepare=$BDD_connexion->prepare($request);
	$prepare->execute();
	$alert = $prepare->fetch(PDO::FETCH_OBJ);
	$alert->location_id = $id_bien;
	
	return $alert;
	
}

function getLocationName($BDD_connexion,$id_bien)
{
	
	$request = "SELECT bien.nom AS location_nom FROM bien WHERE id = :id";
	$prepare = $BDD_connexion->prepare($request);
	$prepare->bindValue(':id',$id_bien,PDO::PARAM_INT);
	$prepare->execute();
	$location = $prepare->fetch(PDO::FETCH_OBJ);

	return $location;
	
}

function getAlertLocationInformations($BDD_connexion,$id_location)
{
	
	$category = getUserRights($BDD_connexion,$_SESSION['userid']);
	
	switch ($category->label)
	{
		
		default:
			$request = "SELECT personne.id AS pers_id, personne.civilite AS pers_civilite, personne.nom AS pers_nom, personne.prenom AS pers_prenom, bien.nom AS location_nom FROM personne,bien,location WHERE location.id_location= :id AND personne.id = location.ref_personne AND bien.id_bien = location.ref_bien";
	
	}
	
	$prepare=$BDD_connexion->prepare($request);
	$prepare->bindValue(':id',$id_location,PDO::PARAM_INT);
	$prepare->execute();
	$alert = $prepare->fetch(PDO::FETCH_OBJ);
	$alert->location_id = $id_location;
	
	return $alert;
		
}

function getAlertProprioId($alerts,$testedAlert,$testedAlertType)
{
	
	foreach ($alerts as $alert)
	{
		if ($alert->type == $testedAlertType && intval($alert->location_id) == $testedAlert)
		{
			return $alert->pers_id;
		}
	}
	
	return -1;
	
}

function buttonDoNotDisplay($language,$alert)
{
	
	return "<form class='alertform' method='POST'>
				<input type='hidden' name='alert_id' value='".intval(intval($alert->location_id))."' />
				<input type='hidden' name='type' value='".$alert->type."' />
				<input type='submit' title='".$language[$_SESSION['lang']]["DoNotDisplayAlert"]."' name='alert_delete' value='X' />
			</form>";
	
}

?>
